<template>
	<!-- <el-dialog
		:title="!dataForm.id ? '新增' : '修改'"
		v-if="isAuth(['ROOT', 'USER:INSERT', 'USER:UPDATE'])"
		:close-on-click-modal="false"
		v-model="visible"
		width="450px"
	></el-dialog> -->
	<el-dialog
	    :title="!dataForm.id ? '新增' : '修改'"
	    v-if="isAuth(['ROOT', 'USER:INSERT', 'USER:UPDATE'])"
	    :close-on-click-modal="false"
	    v-model="visible"
	    width="480px"
	>
	    <el-scrollbar height="500px">
	        <el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="80px">
	            <el-form-item label="用户名" prop="username"><el-input v-model="dataForm.username" clearable /></el-form-item>
				<el-form-item label="昵称" prop="name"><el-input v-model="dataForm.name" clearable /></el-form-item>
	            <el-form-item label="密码" prop="password"><el-input v-model="dataForm.password" clearable /></el-form-item>
	            <el-form-item label="性别">
	                <el-radio-group v-model="dataForm.sex">
	                    <el-radio-button label="男" />
	                    <el-radio-button label="女" />
	                </el-radio-group>
	            </el-form-item>
	            <el-form-item label="电话" prop="tel"><el-input v-model="dataForm.tel" clearable /></el-form-item>
	            <el-form-item label="电子信箱" prop="email">
	                <el-input v-model="dataForm.email" clearable />
	            </el-form-item>
	            <el-form-item label="职务" prop="job">
	                <el-input v-model="dataForm.job" clearable />
	            </el-form-item>
	        </el-form>
	    </el-scrollbar>
	    <template #footer>
	        <span class="dialog-footer">
	            <el-button @click="visible = false">取消</el-button>
	            <el-button type="primary" @click="dataFormSubmit">确定</el-button>
	        </span>
	    </template>
	</el-dialog>
</template>

<script>
import dayjs from 'dayjs';
export default {
	data: function() {
		return {
			visible: false,
			dataForm: {
				id: null,
				createTime: null,
				name: null,
				sex: null,
				tel: null,
				email: null,
				job: null,
				username: null,
				password: null
			},
			dataRule: {
				username: [{ required: true, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{2,10}$', message: '用户名格式错误' }],
				name: [{ required: true, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{2,10}$', message: '昵称格式错误' }],
				password: [{ required: true, message: '填写密码' }],
				tel: [{ required: true, message: '填写电话' }],
				email: [{ required: true, message: '填写邮箱' }],
				job: [{ required: true, message: '填写职业' }]
			}
		};
	},
	methods: {
		reset: function() {
		    let dataForm = {
		        id: null,
		        createTime: null,
		        name: null,
		        sex: null,
		        tel: null,
		        email: null,
		        job: null,
		        username: null,
		        password: null
		    };
		    this.dataForm = dataForm;
		},
		init: function(id) {
		    let that = this;
		    that.reset();
		    that.dataForm.id = id || 0;
		    that.visible = true;
		    that.$nextTick(() => {
		        that.$refs['dataForm'].resetFields();
		        // TODO 查询科室用于修改业务
				if (id) {
				            that.$http('/mis_user/searchById', 'POST', { id: id }, true, function(resp) {
				                that.dataForm.id = resp.id;
				                that.dataForm.username = resp.username;
				                that.dataForm.password = resp.password;
								that.dataForm.name = resp.name;
								that.dataForm.sex = resp.sex;
								that.dataForm.tel = resp.tel;
								that.dataForm.email = resp.email;
								that.dataForm.job = resp.job;
				                that.dataForm.createTime = resp.createTime;
				            });
				        }
		    });
		},
		dataFormSubmit: function() {
		    let that = this;
		    this.$refs['dataForm'].validate(valid => {
		        if (valid) {
		            that.$http(
		                `/mis_user/${!that.dataForm.id ? 'insert' : 'update'}`,
		                'POST',
		                that.dataForm,
		                true,
		                function(resp) {
		                    ElMessage({
		                        message: '操作成功',
		                        type: 'success'
		                    });
		                    that.visible = false;
		                    that.$emit('refreshDataList');
		                }
		            );
		        }
		    });
		}
	}
};
</script>

<style lang="less" scoped="scoped"></style>
